<template>
  <div class="login-wrap">
    <div class="ms-title">BlueHeart后台管理平台</div>
    <div class="ms-login">
     <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
         <el-form-item prop="username">
           <el-input prefix-icon="iconfont icon-user" v-model="ruleForm.username" placeholder="请输入用户名">
             <i class="iconfont icon-user"></i>
           </el-input>
         </el-form-item>
<!--       <span class="iconfont icon-yinsibaohu"></span>-->
       <el-form-item prop="password">
         <el-input type="password"  prefix-icon="iconfont icon-yinsibaohu" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
       </el-form-item>
       <div  class="login-but">
         <el-button type="primary" @click="login(ruleForm.username,ruleForm.password)">登录</el-button>
       </div>
     </el-form>
    </div>
  </div>
</template>
<script>
import { v4 as uuidv4 } from 'uuid';
export default {
  name:'Login',
  data(){
    return{
      ruleForm:{
        username:'',
        password:''
      },
      rules:{
      username:[
          //trigger中的blur是失去焦点，change是改变数据
        {required:true,message:'请输入用户名',trigger:'blur'}
      ],
        password:[
          {required:true,message:'请输入密码',trigger:'blur'}
        ]
      }
    }
  },
  methods:{
    //用户登录
   async login(username,password){
      const result=await this.$API.reqLoginByAdmin(username,password)
      if (result.code==10000){
        localStorage.setItem('name',result.data.name)
        localStorage.setItem('uuid',uuidv4())
        await this.$router.push('home')
        this.$notify.success('登录成功')
      }else {
        this.$notify.error('用户名或密码错误')
      }
    }
  }
}
</script>
<style scoped>
.login-wrap {
  position: relative;
  background: url("../../assets/img/background.jpg") fixed center;
  background-size: cover;
  width: 100%;
  height: 100%;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  font-weight: 600;
  color: #fff;
}
.ms-login{
  position:absolute;
  left: 50%;
  top: 50%;
  width: 320px;
  height: 160px;
  margin-left:-190px;
  margin-top: -150px;
  background: #fff;
  padding: 40px;
  border-radius: 5px;
}
.login-but{
  text-align: center;
}
.login-but button{
  width: 100%;
  height: 36px;
}
</style>
